﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Things.Common;
using LTP.Accounts.Bus;
namespace Things.Web.Farm
{
    public partial class Modify : Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    Guid FarmID = new Guid(Request.Params["id"]);
                    BindUI();
                    ShowInfo(FarmID);
                }
            }
        }

        protected void BindUI()
        {
            Things.BLL.Farmer farmerBLL = new BLL.Farmer();
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append(" IsDeleted=0 ");
            ds = farmerBLL.GetList(strWhere.ToString());
            Things.Common.UICommon.BindDDL(ddlFarmer, ds, "FarmerName", "FarmerID");
        }

        private void ShowInfo(Guid FarmID)
        {
            Things.BLL.Farm bll = new Things.BLL.Farm();
            Things.Model.Farm model = bll.GetModel(FarmID);
            this.lblFarmID.Text = model.FarmID.ToString();
            this.ddlFarmer.SelectedValue = model.FarmerID.ToString();
            this.txtAddress.Text = model.Address;
            this.txtCoordinate.Text = model.Coordinate;

        }

        public void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (this.ddlFarmer.SelectedValue == "-1")
            {
                strErr += "请选择农户！\\n";
            }
            if (this.txtAddress.Text.Trim().Length == 0)
            {
                strErr += "农田地址不能为空！\\n";
            }
            if (this.txtCoordinate.Text.Trim().Length == 0)
            {
                strErr += "农田坐标不能为空！\\n";
            }
            Guid farmerID = Guid.Empty;
            try
            {
                farmerID = new Guid(this.ddlFarmer.SelectedValue);
            }
            catch
            {
                strErr += "农户信息有误！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            Guid FarmID = new Guid(this.lblFarmID.Text);
            string Address = this.txtAddress.Text;
            string Coordinate = this.txtCoordinate.Text;
            bool IsDeleted = false;

            Things.Model.Farm model = new Things.Model.Farm();
            model.FarmID = FarmID;
            model.FarmerID = farmerID;
            model.Address = Address;
            model.Coordinate = Coordinate;
            model.IsDeleted = IsDeleted;

            Things.BLL.Farm bll = new Things.BLL.Farm();
            bll.Update(model);
            Things.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "list.aspx");

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }
    }
}
